package pro1.service.impl;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import pro1.dao.UserMapper;
import pro1.model.ResponseBody;
import pro1.model.User;
import pro1.service.LoginService;
import pro1.util.MD5Util;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	UserMapper userMapper;

	@Override
	public ResponseBody login(HttpSession session, String userName, String pwd, Integer type) {
		ResponseBody response = new ResponseBody(0, "账号或密码错误");
		User user = userMapper.selectByUserName(userName);
		if (user != null) {
			if (user.getRole() == type && MD5Util.MD5Encode(pwd, null).equals(user.getPassword())) {
				// session保存登陆凭证
				session.setAttribute("user", user);
				response = new ResponseBody(1, "登陆成功");
			}
		}
		return response;
	}

}
